@echo off
chcp 65001 >nul
title RocketMQ项目稳定部署

echo ========================================
echo    RocketMQ项目稳定部署脚本
echo ========================================
echo.

REM 设置变量
set SERVER_IP=192.168.211.139
set SERVER_USER=root
set SSH_KEY=C:\Users\Teriberka\.ssh\my_linux_key
set PROJECT_NAME=rocketmq-rate-limit-demo

echo 服务器信息:
echo - IP地址: %SERVER_IP%
echo - 用户名: %SERVER_USER%
echo - SSH密钥: %SSH_KEY%
echo.

echo 开始部署流程...
echo.

REM 步骤1: 检查Maven
echo [步骤1] 检查Maven环境...
mvn --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ✗ Maven检查失败
    pause
    exit /b 1
)
echo ✓ Maven检查通过
echo.

REM 步骤2: 清理并构建项目
echo [步骤2] 构建项目...
echo 执行: mvn clean package -DskipTests

REM 创建日志文件
set LOG_FILE=deploy_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%.log
set LOG_FILE=%LOG_FILE: =0%

echo 构建日志将保存到: %LOG_FILE%
echo.

mvn clean package -DskipTests > %LOG_FILE% 2>&1
set BUILD_RESULT=%errorlevel%

REM 显示构建结果
if %BUILD_RESULT% neq 0 (
    echo ✗ 项目构建失败
    echo 请查看日志文件: %LOG_FILE%
    echo.
    echo 最后10行日志:
    echo ----------------------------------------
    powershell -Command "Get-Content '%LOG_FILE%' | Select-Object -Last 10"
    echo ----------------------------------------
    pause
    exit /b 1
)

echo ✓ 项目构建成功
echo.

REM 步骤3: 检查jar包
echo [步骤3] 检查构建结果...
if not exist target\%PROJECT_NAME-*.jar (
    echo ✗ 未找到构建的jar包
    echo 请检查target目录:
    dir target
    pause
    exit /b 1
)

REM 获取jar包名称
for %%f in (target\%PROJECT_NAME-*.jar) do set JAR_FILE=%%f
echo ✓ 找到jar包: %JAR_FILE%
echo.

REM 步骤4: 准备部署文件
echo [步骤4] 准备部署文件...
if exist deploy rmdir /s /q deploy
mkdir deploy

copy "%JAR_FILE%" deploy\app.jar >nul
if not exist deploy\app.jar (
    echo ✗ 复制jar包失败
    pause
    exit /b 1
)

copy Dockerfile deploy\ >nul
if not exist deploy\Dockerfile (
    echo ✗ 复制Dockerfile失败
    pause
    exit /b 1
)

copy broker.conf deploy\ >nul
if not exist deploy\broker.conf (
    echo ✗ 复制broker.conf失败
    pause
    exit /b 1
)

echo ✓ 部署文件准备完成
echo 部署目录内容:
dir deploy
echo.

REM 步骤5: 上传到服务器
echo [步骤5] 上传文件到服务器...
echo 执行: scp -i "%SSH_KEY%" -r deploy\* %SERVER_USER%@%SERVER_IP%:/home/rocketmq/

scp -i "%SSH_KEY%" -r deploy\* %SERVER_USER%@%SERVER_IP%:/home/rocketmq/ >nul 2>&1
if %errorlevel% neq 0 (
    echo ✗ 文件上传失败
    echo 请检查:
    echo 1. 网络连接是否正常
    echo 2. SSH密钥是否正确
    echo 3. 服务器是否可访问
    pause
    exit /b 1
)

echo ✓ 文件上传成功
echo.

REM 步骤6: 在服务器上部署
echo [步骤6] 在服务器上构建和部署...
echo 执行: ssh -i "%SSH_KEY%" %SERVER_USER%@%SERVER_IP%

REM 创建服务器端部署脚本
echo 创建服务器端部署脚本...
(
echo #!/bin/bash
echo cd /home/rocketmq
echo.
echo echo "停止现有容器..."
echo docker stop rocketmq-app 2^>/dev/null ^|^| true
echo docker rm rocketmq-app 2^>/dev/null ^|^| true
echo.
echo echo "删除旧镜像..."
echo docker rmi rocketmq-app:latest 2^>/dev/null ^|^| true
echo.
echo echo "构建新镜像..."
echo docker build -t rocketmq-app:latest .
echo.
echo if [ $? -eq 0 ]; then
echo     echo "镜像构建成功，启动应用容器..."
echo     docker run -d \
echo       --name rocketmq-app \
echo       --network rmq \
echo       -p 8080:8080 \
echo       -e SPRING_PROFILES_ACTIVE=prod \
echo       -e ROCKETMQ_NAMESRV_ADDR=192.168.211.139:9876 \
echo       -v /home/rocketmq/logs/app:/app/logs \
echo       --restart unless-stopped \
echo       rocketmq-app:latest
echo.
echo     echo "等待应用启动..."
echo     sleep 15
echo.
echo     echo "检查容器状态..."
echo     docker ps ^| grep rocketmq-app
echo.
echo     echo "测试应用健康状态..."
echo     if curl -f http://localhost:8080/api/rocketmq/health ^>/dev/null 2^>^&1; then
echo         echo "✓ 应用健康检查通过！"
echo     else
echo         echo "⚠ 应用还未完全启动，请稍后检查"
echo     fi
echo.
echo     echo "清理临时文件..."
echo     rm -f app.jar Dockerfile broker.conf
echo else
echo     echo "✗ 镜像构建失败！"
echo     exit 1
echo fi
) > deploy-script.sh

echo ✓ 服务器端部署脚本创建完成
echo.

REM 上传并执行部署脚本
echo 上传部署脚本到服务器...
scp -i "%SSH_KEY%" deploy-script.sh %SERVER_USER%@%SERVER_IP%:/home/rocketmq/ >nul 2>&1

echo 在服务器上执行部署...
ssh -i "%SSH_KEY%" %SERVER_USER%@%SERVER_IP% "chmod +x /home/rocketmq/deploy-script.sh && /home/rocketmq/deploy-script.sh"

if %errorlevel% neq 0 (
    echo ✗ 服务器部署失败
    echo 请检查服务器上的Docker环境
    pause
    exit /b 1
)

echo ✓ 服务器部署成功
echo.

REM 步骤7: 清理临时文件
echo [步骤7] 清理临时文件...
rmdir /s /q deploy
del deploy-script.sh >nul 2>&1
del %LOG_FILE% >nul 2>&1
echo ✓ 清理完成
echo.

echo ========================================
echo           部署完成！
echo ========================================
echo.
echo 应用访问地址: http://%SERVER_IP%:8080
echo 健康检查: http://%SERVER_IP%:8080/api/rocketmq/health
echo.
echo 部署成功！按任意键退出...
pause >nul

